﻿
// BayesDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "Bayes.h"
#include "BayesDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

   #pragma warning(disable:4996)


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CBayesDlg 对话框



CBayesDlg::CBayesDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_BAYES_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBayesDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BUTTON_EDIT, m_btnedit);
	DDX_Control(pDX, IDC_BUTTON_SHPIN, m_btneditshp);
	DDX_Control(pDX, IDC_RADIO_3, m_Radio);
	DDX_Control(pDX, IDC_BUTTON_RUN, m_btnrun);
	DDX_Control(pDX, IDC_EDIT_EMTIMES, mEMtimes);
	DDX_Control(pDX, IDC_EDIT_EMCHANGERATE, mEMchangerate);
	DDX_Control(pDX, IDC_EDIT_FCMTIMES, mFCMtimes);
	DDX_Control(pDX, IDC_EDIT_FCMCHANGERATE, mFCMchangerate);
	DDX_Control(pDX, IDC_EDIT_TIMES, mtimes);
	DDX_Control(pDX, IDC_EDIT_CHANGERATE, mchangerate);
	DDX_Control(pDX, IDC_RADIO_1, mModel1);
	DDX_Control(pDX, IDC_RADIO_2, mModel2);
	DDX_Control(pDX, IDC_EDIT_LOSS_11, mLoss11);
	DDX_Control(pDX, IDC_EDIT_LOSS_12, mLoss12);
	DDX_Control(pDX, IDC_EDIT_LOSS_13, mLoss13);
	DDX_Control(pDX, IDC_EDIT_LOSS_14, mLoss14);
	DDX_Control(pDX, IDC_EDIT_LOSS_15, mLoss15);
	DDX_Control(pDX, IDC_EDIT_LOSS_21, mLoss21);
	DDX_Control(pDX, IDC_EDIT_LOSS_22, mLoss22);
	DDX_Control(pDX, IDC_EDIT_LOSS_23, mLoss23);
	DDX_Control(pDX, IDC_EDIT_LOSS_24, mLoss24);
	DDX_Control(pDX, IDC_EDIT_LOSS_25, mLoss25);
	DDX_Control(pDX, IDC_EDIT_LOSS_31, mLoss31);
	DDX_Control(pDX, IDC_EDIT_LOSS_32, mLoss32);
	DDX_Control(pDX, IDC_EDIT_LOSS_33, mLoss33);
	DDX_Control(pDX, IDC_EDIT_LOSS_34, mLoss34);
	DDX_Control(pDX, IDC_EDIT_LOSS_35, mLoss35);
	DDX_Control(pDX, IDC_EDIT_LOSS_41, mLoss41);
	DDX_Control(pDX, IDC_EDIT_LOSS_42, mLoss42);
	DDX_Control(pDX, IDC_EDIT_LOSS_43, mLoss43);
	DDX_Control(pDX, IDC_EDIT_LOSS_44, mLoss44);
	DDX_Control(pDX, IDC_EDIT_LOSS_45, mLoss45);
	DDX_Control(pDX, IDC_EDIT_LOSS_51, mLoss51);
	DDX_Control(pDX, IDC_EDIT_LOSS_52, mLoss52);
	DDX_Control(pDX, IDC_EDIT_LOSS_53, mLoss53);
	DDX_Control(pDX, IDC_EDIT_LOSS_54, mLoss54);
	DDX_Control(pDX, IDC_EDIT_LOSS_55, mLoss55);
	DDX_Control(pDX, IDC_CHECK_EM, mEMbutton);
}

BEGIN_MESSAGE_MAP(CBayesDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_TIFIN, &CBayesDlg::OnBnClickedButtonTifin)
	ON_BN_CLICKED(IDC_BUTTON_SHPIN, &CBayesDlg::OnBnClickedButtonShpin)
	ON_BN_CLICKED(IDC_BUTTON_RUN, &CBayesDlg::OnBnClickedButtonRun)
	ON_BN_CLICKED(IDC_BUTTON_EDIT, &CBayesDlg::OnBnClickedButtonEdit)
	ON_BN_CLICKED(IDC_RADIO_1, &CBayesDlg::OnBnClickedRadio1)
	ON_BN_CLICKED(IDC_RADIO_2, &CBayesDlg::OnBnClickedRadio2)
	ON_BN_CLICKED(IDC_RADIO_3, &CBayesDlg::OnBnClickedRadio3)
	ON_BN_CLICKED(IDC_CHECK_EM, &CBayesDlg::OnBnClickedCheckEm)
	ON_BN_CLICKED(IDC_CHECK_FCM, &CBayesDlg::OnBnClickedCheckFcm)
END_MESSAGE_MAP()


// CBayesDlg 消息处理程序

BOOL CBayesDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	pProg = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS);
	pProg->SetRange(0, 100);//设置进度条范围
	pProg->SetPos(0);//设置进度条初始位置

	pProg->SetBarColor(RGB(1, 132, 127));//设置进度条前景色为马尔斯绿

	m_btnedit.EnableWindow(0);//设置修改训练集的按钮不可用
	m_btneditshp.EnableWindow(0);//设置输入矢量的按钮不可用
	m_btnrun.EnableWindow(0);//设置输入矢量的按钮不可用
	mEMtimes.EnableWindow(0);//设置修改训练集的按钮可用
	mEMchangerate.EnableWindow(0);//设置修改训练集的按钮可用

	EMflag = 0;
	FCMflag = 0;

	//初始化GDAL
	GDALAllRegister();
	CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");//使得GDAL不默认使用UTF8编码，以支持中文路径
	CPLSetConfigOption("SHAPE_ENCODING", "");//使得GDAL读取矢量文件支持中文

	// TODO: 在此添加额外的初始化代码

	ModelFlag = 3;
	m_Radio.SetCheck(TRUE);

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CBayesDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CBayesDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CBayesDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}





void CBayesDlg::OnBnClickedButtonTifin()
{
	SetWindowText("Bayes分类（打开栅格文件）");

	LPCTSTR szFilter = _T("TIF files(*.tif)|*.tif|");		//提示保存文件的类型
	CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilter);		// CFileDialog的第一个参数标明是浏览(打开，TRUE)还是保存（FALSE）

	if (dlg.DoModal() == IDOK)		//返回值是OK，说明点击了“保存”/“确认”按钮，返回值是IDCANCEL，说明点击了取消
	{
		sTifPathIn = dlg.GetPathName();		//保存文件的路径到filename变量中
	}

	if (sTifPathIn == "")
	{
		MessageBox("读取文件路径为空！");
		return;
	}

	SetDlgItemTextA(IDC_EDIT_TIFIN, sTifPathIn);

	SetWindowText("Bayes分类");

	Scan.OpenRaster(sTifPathIn);

	BayesDe.Malloc_PData_Func(Scan.RasterR.iHeight);

	for (int ii = 0; ii < Scan.RasterR.iHeight; ii++)
	{
		BayesDe.Malloc_PData_Func(ii, Scan.RasterR.iWidth, 80);
		int pro = int(100.0 / (double)Scan.RasterR.iHeight * (double)(ii + 1));//计算进度条进度
		pProg->SetPos(pro);//设置新的进度条进度
	}

	pProg->SetPos(0);//设置新的进度条进度

	AfxMessageBox("读取栅格信息成功");

	m_btneditshp.EnableWindow(1);//设置输入矢量的按钮可用
}


void CBayesDlg::OnBnClickedButtonShpin()
{
	SetWindowText("Bayes分类（打开矢量文件）");

	LPCTSTR szFilter = _T("SHP files(*.shp)|*.shp|");		//提示保存文件的类型
	CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilter);		// CFileDialog的第一个参数标明是浏览(打开，TRUE)还是保存（FALSE）

	if (dlg.DoModal() == IDOK)		//返回值是OK，说明点击了“保存”/“确认”按钮，返回值是IDCANCEL，说明点击了取消
	{
		sShpPathIn = dlg.GetPathName();		//保存文件的路径到filename变量中
	}

	if (sShpPathIn == "")
	{
		MessageBox("读取文件路径为空！");
		return;
	}

	SetDlgItemTextA(IDC_EDIT_SHPIN, sShpPathIn);

	SetDlgItemTextA(IDC_EDIT_RATE, "80");

	{
		SetDlgItemTextA(IDC_EDIT_LOSS_11, "0");
		SetDlgItemTextA(IDC_EDIT_LOSS_12, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_13, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_14, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_15, "1");

		SetDlgItemTextA(IDC_EDIT_LOSS_21, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_22, "0");
		SetDlgItemTextA(IDC_EDIT_LOSS_23, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_24, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_25, "1");

		SetDlgItemTextA(IDC_EDIT_LOSS_31, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_32, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_33, "0");
		SetDlgItemTextA(IDC_EDIT_LOSS_34, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_35, "1");

		SetDlgItemTextA(IDC_EDIT_LOSS_41, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_42, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_43, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_44, "0");
		SetDlgItemTextA(IDC_EDIT_LOSS_45, "1");

		SetDlgItemTextA(IDC_EDIT_LOSS_51, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_52, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_53, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_54, "1");
		SetDlgItemTextA(IDC_EDIT_LOSS_55, "0");

		SetDlgItemTextA(IDC_EDIT_TIMES, "1");
		SetDlgItemTextA(IDC_EDIT_CHANGERATE, "10");
		SetDlgItemTextA(IDC_EDIT_EMTIMES, "1");
		SetDlgItemTextA(IDC_EDIT_EMCHANGERATE, "10");
		SetDlgItemTextA(IDC_EDIT_FCMTIMES, "1");
		SetDlgItemTextA(IDC_EDIT_FCMCHANGERATE, "10");
	}
	
	m_btnedit.EnableWindow(1);//设置修改训练集的按钮可用
	m_btnrun.EnableWindow(1);//设置输入矢量的按钮不可用

	SetWindowText("Bayes分类");
}

BOOL CBayesDlg::ReadFile()
{
	GetDlgItemTextA(IDC_TIFOUT, sTifPathOut);
	if (sTifPathOut == "")
	{
		AfxMessageBox("保存路径为空，请选择保存路径");
		return 0;
	}

	int iPolygon = Scan.OpenFile(sShpPathIn, sTifPathIn, sTifPathOut);
	//打开矢量文件，并读取多边形的数量

	int pro = 0;//设置进度条到0

	for (int ii = 0; ii < iPolygon; ii++)
	{
		pro = int(100.0 / (double)iPolygon * (double)(ii + 1));//计算进度条进度
		Scan.Classify(ii);//分类第ii个多边形
		pProg->SetPos(pro);//设置新的进度条进度
	}

	Scan.Release();//释放资源

	pProg->SetPos(0);//进度条回归0

	return 1;
}

void CBayesDlg::OnBnClickedButtonRun()
{
	int a = ReadFile();
	if (a == 1)
	{
		if (FCMflag == 1)
		{
			BayesDe.Malloc_pLData(sTifPathIn);
			BayesDe.Malloc_pCData(sTifPathOut);
			BayesDe.Malloc_MeanValueMat(5, Scan.RasterR.iBandNum);
			BayesDe.pCData = BayesDe.RasterC.ReadRaster(1, BayesDe.pCData);
			Get_Mean_Value();

			FCMeans.Malloc_pLData(sTifPathIn);
			FCMeans.Malloc_pUData(5);
			FCMeans.Malloc_pWData(5);
			FCMeans.Malloc_UX(5, 3);
			FCMeans.Create_RasterC(sTifPathOut, 5);
			FCMeans.MeanValueMatFCM = BayesDe.MeanValueMat;
			FCMeans.pCData = BayesDe.pCData;
			FCMeans.pPData = BayesDe.pPData;

			CString sTimes, sChangeRate;
			int iTimes, iChangeRate;
			GetDlgItemTextA(IDC_EDIT_FCMTIMES, sTimes);
			GetDlgItemTextA(IDC_EDIT_FCMCHANGERATE, sChangeRate);
			iTimes = _ttoi(sTimes);
			iChangeRate = _ttoi(sChangeRate);

			for (int Times = 0; Times < iTimes; Times++)
			{
				FCMeans.Initialize(Times);

				if (Times != 0)
				{
					SetWindowText("FCM分类(更新聚类中心)");
					FCMeans.UpData_pCData();
					for (int ii = 0; ii < FCMeans.RasterR.iHeight; ii++)
					{
						FCMeans.UpData_Mean(ii);
						double pro = int(100.0 / (double)BayesDe.RasterR.iHeight * (double)(ii + 1));//计算进度条进度
						pProg->SetPos(pro);//设置新的进度条进度
					}
					pProg->SetPos(0);//设置新的进度条进度
					FCMeans.UpData_Mean();
				}
				
				SetWindowText("FCM分类(计算各个像元归属)");
				for (int ii = 0; ii < FCMeans.RasterR.iHeight; ii++)
				{
					FCMeans.FuzzyCMeans_Run(sTifPathOut, ii);
					double pro = int(100.0 / (double)BayesDe.RasterR.iHeight * (double)(ii + 1));//计算进度条进度
					pProg->SetPos(pro);//设置新的进度条进度
				}
				FCMeans.RasterW.WriteRaster(1, FCMeans.pWData, FCMeans.RasterR.iHeight, FCMeans.RasterR.iWidth);
				pProg->SetPos(0);//设置新的进度条进度

				if (Times != 0)
				{
					double dChangeRate = (FCMeans.Loss_Function - FCMeans.Loss_Function_Old) / FCMeans.Loss_Function;
					CString aaaa;
					aaaa.Format("%.2f", FCMeans.Loss_Function);
					AfxMessageBox(aaaa);
					aaaa.Format("%.2f", FCMeans.Loss_Function_Old);
					AfxMessageBox(aaaa);
					aaaa.Format("%.2f", dChangeRate);
					AfxMessageBox(aaaa);
					if (dChangeRate < (double)iChangeRate / 100 && dChangeRate >(double)0 - iChangeRate / 100)
					{
						break;
					}
				}
				
			}
			
			FCMeans.Release();
		}
		else
		{
			{
				CString sTempLoss;
				double dTempLoss;

				GetDlgItemTextA(IDC_EDIT_LOSS_11, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[0][0] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_12, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[0][1] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_13, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[0][2] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_14, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[0][3] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_15, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[0][4] = dTempLoss;

				GetDlgItemTextA(IDC_EDIT_LOSS_21, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[1][0] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_22, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[1][1] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_23, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[1][2] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_24, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[1][3] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_25, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[1][4] = dTempLoss;

				GetDlgItemTextA(IDC_EDIT_LOSS_31, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[2][0] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_32, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[2][1] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_33, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[2][2] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_34, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[2][3] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_35, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[2][4] = dTempLoss;

				GetDlgItemTextA(IDC_EDIT_LOSS_41, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[3][0] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_42, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[3][1] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_43, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[3][2] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_44, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[3][3] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_45, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[3][4] = dTempLoss;

				GetDlgItemTextA(IDC_EDIT_LOSS_51, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[4][0] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_52, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[4][1] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_53, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[4][2] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_54, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[4][3] = dTempLoss;
				GetDlgItemTextA(IDC_EDIT_LOSS_55, sTempLoss);
				dTempLoss = _ttof(sTempLoss);
				BayesDe.Loss[4][4] = dTempLoss;

				for (int ii = 0; ii < 5; ii++)
				{
					for (int jj = 0; jj < 5; jj++)
					{
						if (BayesDe.Loss[ii][jj] < 0.0)
						{
							CString sPositionii;
							sPositionii.Format("%d", ii + 1);
							CString sPositionjj;
							sPositionjj.Format("%d", jj + 1);
							CString sPosition;
							sPosition = "损失矩阵中第" + sPositionii + "行，第" + sPositionjj + "列数据出错，应当大于0";
							AfxMessageBox(sPosition);
							return;
						}
						if (BayesDe.Loss[ii][jj] > 1.0)
						{
							CString sPositionii;
							sPositionii.Format("%d", ii + 1);
							CString sPositionjj;
							sPositionjj.Format("%d", jj + 1);
							CString sPosition;
							sPosition = "损失矩阵中第" + sPositionii + "行，第" + sPositionjj + "列数据出错，应当小于1";
							AfxMessageBox(sPosition);
							return;
						}
					}
				}
			}

			BayesDe.Malloc_pLData(sTifPathIn);
			BayesDe.Malloc_pCData(sTifPathOut);
			BayesDe.Malloc_MeanValueMat(5, Scan.RasterR.iBandNum);

			CString sTimes, sChangeRate;
			int iTimes, iChangeRate;
			GetDlgItemTextA(IDC_EDIT_TIMES, sTimes);
			GetDlgItemTextA(IDC_EDIT_CHANGERATE, sChangeRate);
			iTimes = _ttoi(sTimes);
			iChangeRate = _ttoi(sChangeRate);

			for (int ii = 0; ii < iTimes; ii++)
			{
				if (ii == 0)
				{
					BayesDe.pCData = BayesDe.RasterC.ReadRaster(1, BayesDe.pCData);
				}

				Get_Mean_Value();
				Get_Con_Mat();
				Get_Class();

				CString aaa;
				aaa.Format("%.2f", BayesDe.ChangeNum / BayesDe.RasterR.iWidth / BayesDe.RasterR.iHeight);
				//AfxMessageBox(aaa);

				if (BayesDe.ChangeNum / BayesDe.RasterR.iWidth / BayesDe.RasterR.iHeight < iChangeRate / 100)
				{
					break;
				}
			}

			Write_Out_Result();
		}

		AfxMessageBox("Done");
	}
}


void CBayesDlg::OnBnClickedButtonEdit()
{
	pDC = this->GetDC();
	TrainRate.DoModal();//打开训练集比例设置对话框

	if (TrainRate.sRate == "")
	{
		return;
	}

	int iRate = _ttoi(TrainRate.sRate);//将比例转化为int类型

	if (iRate <= 0)
	{
		AfxMessageBox("训练集比例需要大于0");
		return;
	}

	if (iRate >= 100)
	{
		AfxMessageBox("训练集比例需要小于100");
		return;
	}

	SetDlgItemTextA(IDC_EDIT_RATE, TrainRate.sRate);//读取训练集比例

	if(BayesDe.pPData != NULL)
		BayesDe.Free_PData_Func(Scan.RasterR.iHeight, Scan.RasterR.iWidth);
	BayesDe.Malloc_PData_Func(Scan.RasterR.iHeight);

	for (int ii = 0; ii < Scan.RasterR.iHeight; ii++)
	{
		BayesDe.Malloc_PData_Func(ii, Scan.RasterR.iWidth, iRate);
		int pro = int(100.0 / (double)Scan.RasterR.iHeight * (double)(ii + 1));//计算进度条进度
		pProg->SetPos(pro);//设置新的进度条进度
	}

	pProg->SetPos(0);//设置新的进度条进度

	AfxMessageBox("重定义训练集占比成功");
}

BOOL CBayesDlg::Get_Mean_Value()
{
	SetWindowText("Bayes分类（计算均值向量）");

	int iRate;
	CString sRate;

	GetDlgItemTextA(IDC_EDIT_RATE, sRate);
	iRate = _ttoi(sRate);

	BayesDe.MeanValueMat = BayesDe.Get_Mean_Value(BayesDe.MeanValueMat, iRate);

	return 0;
}

BOOL CBayesDlg::Get_Con_Mat()
{
	SetWindowText("Bayes分类（计算协方差矩阵）");

	if (ModelFlag == 1)
	{
		return 0;
	}
	if (ModelFlag == 2)
	{
		BayesDe.Malloc_ConMat(5, Scan.RasterR.iBandNum);
		BayesDe.ConMat = BayesDe.Get_Covariance_Matrix2(BayesDe.MeanValueMat, BayesDe.ConMat);
	}
	if (ModelFlag == 3)
	{
		BayesDe.Malloc_ConMat(5, Scan.RasterR.iBandNum);
		BayesDe.ConMat = BayesDe.Get_Covariance_Matrix3(BayesDe.MeanValueMat, BayesDe.ConMat);
	}

	return 0;
}

BOOL CBayesDlg::Get_Class()
{
	if (ModelFlag == 1)
	{
		BayesDe.Get_Mean_Result();

		for (int ii = 0; ii < BayesDe.RasterR.iHeight; ii++)
		{
			BayesDe.Bayes_Decision_Func1(BayesDe.MeanValueMat, ii);
			double pro = int(100.0 / (double)BayesDe.RasterR.iHeight * (double)(ii + 1));//计算进度条进度
			pProg->SetPos(pro);//设置新的进度条进度
		}

		pProg->SetPos(0);//设置新的进度条进度
	}
	else if (ModelFlag == 2)
	{
		BayesDe.Malloc_XMat();
		BayesDe.Malloc_XMatT();

		BayesDe.Malloc_ConMat_1(5, 3);

		BayesDe.Get_Reverse_Matrix_of_Covariance_Matrix(BayesDe.ConMat, BayesDe.ConMat_1);
		BayesDe.Get_Determinant_Matrix_of_Covariance_Matrix(BayesDe.ConMat, BayesDe.MatResult);

		if (EMflag == 1)
		{
			CString sEMTimes;
			CString sEMChangeRate;
			int iEMTimes;
			int iEMChangeRate;

			GetDlgItemTextA(IDC_EDIT_EMTIMES, sEMTimes);
			GetDlgItemTextA(IDC_EDIT_EMCHANGERATE, sEMChangeRate);
			iEMTimes = _ttoi(sEMTimes);
			iEMChangeRate = _ttoi(sEMChangeRate);

			{
				CString FilePathOut = sTifPathOut + "\\MeanValue1.txt";

				CFile file;
				file.Open(FilePathOut, CFile::modeWrite | CFile::modeCreate); //如果不存在就创建

				if (file == NULL)
				{
					AfxMessageBox(_T("Open File ERROR"));
				}
				CString sss, aaa;
				sss = "";
				for (int ii = 0; ii < 5; ii++)
				{
					if (sss == "")
					{
						sss = "iDef = 1 农田：";
					}
					else if (sss == "iDef = 1 农田：")
					{
						sss = "iDef = 2 水体：";
					}
					else if (sss == "iDef = 2 水体：")
					{
						sss = "iDef = 3 建筑：";
					}
					else if (sss == "iDef = 3 建筑：")
					{
						sss = "iDef = 4 道路：";
					}
					else if (sss == "iDef = 4 道路：")
					{
						sss = "iDef = 5 其他：";
					}
					int nLengths = sss.GetLength();
					char *cs = new char[nLengths + 1];
					::memset(cs, 0, sizeof(cs));
					::sprintf(cs, "%s", sss);
					file.Write(cs, strlen(cs));
					double * TempMeanValue = BayesDe.MeanValueMat[ii];

					for (int jj = 0; jj < 3; jj++)
					{
						aaa.Format("%.2f", TempMeanValue[jj]);
						int nLength = aaa.GetLength();
						char *c = new char[nLength + 1];
						::memset(c, 0, sizeof(c));
						::sprintf(c, "%s", aaa);
						file.Write(c, strlen(c));
						for (int oo = 0; oo < 20 - nLength; oo++)
						{
							char *e = new char[2];
							::memset(e, 0, sizeof(e));
							::sprintf(e, "%s", " ");
							file.Write(e, strlen(e));
						}

					}
					char *WriteBuf = "\r\n";
					file.Write(WriteBuf, strlen(WriteBuf));
					file.Write(WriteBuf, strlen(WriteBuf));

				}
			}
			{
				CString FilePathOut = sTifPathOut + "\\ConMat1.txt";

				CFile file;
				file.Open(FilePathOut, CFile::modeWrite | CFile::modeCreate); //如果不存在就创建

				if (file == NULL)
				{
					AfxMessageBox(_T("Open File ERROR"));
				}
				CString sss, aaa;
				sss = "";
				for (int ii = 0; ii < 5; ii++)
				{
					if (sss == "")
					{
						sss = "iDef = 1 农田：";
					}
					else if (sss == "iDef = 1 农田：")
					{
						sss = "iDef = 2 水体：";
					}
					else if (sss == "iDef = 2 水体：")
					{
						sss = "iDef = 3 建筑：";
					}
					else if (sss == "iDef = 3 建筑：")
					{
						sss = "iDef = 4 道路：";
					}
					else if (sss == "iDef = 4 道路：")
					{
						sss = "iDef = 5 其他：";
					}
					int nLengths = sss.GetLength();
					char *cs = new char[nLengths + 1];
					::memset(cs, 0, sizeof(cs));
					::sprintf(cs, "%s", sss);
					file.Write(cs, strlen(cs));
					double ** TempCon = BayesDe.ConMat[ii];
					for (int jj = 0; jj < 3; jj++)
					{
						double * TempTempCon = TempCon[jj];
						for (int kk = 0; kk < 3; kk++)
						{
							aaa.Format("%.2f", TempTempCon[kk]);
							int nLength = aaa.GetLength();
							char *c = new char[nLength + 1];
							::memset(c, 0, sizeof(c));
							::sprintf(c, "%s", aaa);
							file.Write(c, strlen(c));
							for (int oo = 0; oo < 20 - nLength; oo++)
							{
								char *e = new char[2];
								::memset(e, 0, sizeof(e));
								::sprintf(e, "%s", " ");
								file.Write(e, strlen(e));
							}
						}
					}
					char *WriteBuf = "\r\n";
					file.Write(WriteBuf, strlen(WriteBuf));
					file.Write(WriteBuf, strlen(WriteBuf));
				}
			}

			SetWindowText("Bayes分类（高斯混合模型修正）");

			for (int Times = 0; Times < iEMTimes; Times++)
			{
				ExMax.Malloc_pLData(sTifPathIn);
				ExMax.Malloc_MeanValueMat(5, 3);
				ExMax.Malloc_ConMat(5, 3);

				ExMax.Initialize();

				double EndFlag = 0;
				for (int ii = 0; ii < BayesDe.RasterR.iHeight; ii++)
				{
					ExMax.ExpectationMaximization(BayesDe.MeanValueMat, BayesDe.ConMat_1, BayesDe.MatResult, BayesDe.XMat, BayesDe.XMatT, BayesDe.Rate, BayesDe.pCData, BayesDe.pPData, ii);
					double pro = int(100.0 / (double)BayesDe.RasterR.iHeight * (double)(ii + 1));//计算进度条进度
					pProg->SetPos(pro);//设置新的进度条进度
				}

				ExMax.Result();

				BayesDe.MeanValueMat = ExMax.MeanValueMatEM;
				BayesDe.ConMat = ExMax.ConMatEM;

				pProg->SetPos(0);//设置新的进度条进度

				for (int ii = 0; ii < 5; ii++)
				{

					EndFlag += fabs((BayesDe.Rate[ii] - ExMax.RateEM[ii]) / BayesDe.Rate[ii] * 100);

					BayesDe.Rate[ii] = ExMax.RateEM[ii];
				}

				if (EndFlag <= 5 * iEMChangeRate && EndFlag >= -5 * iEMChangeRate)
				{
					break;
				}
			}

			BayesDe.Get_Reverse_Matrix_of_Covariance_Matrix(BayesDe.ConMat, BayesDe.ConMat_1);
			BayesDe.Get_Determinant_Matrix_of_Covariance_Matrix(BayesDe.ConMat, BayesDe.MatResult);

			{
				CString FilePathOut = sTifPathOut + "\\MeanValue2.txt";

				CFile file;
				file.Open(FilePathOut, CFile::modeWrite | CFile::modeCreate); //如果不存在就创建

				if (file == NULL)
				{
					AfxMessageBox(_T("Open File ERROR"));
				}
				CString sss, aaa;
				sss = "";
				for (int ii = 0; ii < 5; ii++)
				{
					if (sss == "")
					{
						sss = "iDef = 1 农田：";
					}
					else if (sss == "iDef = 1 农田：")
					{
						sss = "iDef = 2 水体：";
					}
					else if (sss == "iDef = 2 水体：")
					{
						sss = "iDef = 3 建筑：";
					}
					else if (sss == "iDef = 3 建筑：")
					{
						sss = "iDef = 4 道路：";
					}
					else if (sss == "iDef = 4 道路：")
					{
						sss = "iDef = 5 其他：";
					}
					int nLengths = sss.GetLength();
					char *cs = new char[nLengths + 1];
					::memset(cs, 0, sizeof(cs));
					::sprintf(cs, "%s", sss);
					file.Write(cs, strlen(cs));
					double * TempMeanValue = BayesDe.MeanValueMat[ii];

					for (int jj = 0; jj < 3; jj++)
					{
						aaa.Format("%.2f", TempMeanValue[jj]);
						int nLength = aaa.GetLength();
						char *c = new char[nLength + 1];
						::memset(c, 0, sizeof(c));
						::sprintf(c, "%s", aaa);
						file.Write(c, strlen(c));
						for (int oo = 0; oo < 20 - nLength; oo++)
						{
							char *e = new char[2];
							::memset(e, 0, sizeof(e));
							::sprintf(e, "%s", " ");
							file.Write(e, strlen(e));
						}

					}
					char *WriteBuf = "\r\n";
					file.Write(WriteBuf, strlen(WriteBuf));
					file.Write(WriteBuf, strlen(WriteBuf));

				}
			}
			{
				CString FilePathOut = sTifPathOut + "\\ConMat2.txt";

				CFile file;
				file.Open(FilePathOut, CFile::modeWrite | CFile::modeCreate); //如果不存在就创建

				if (file == NULL)
				{
					AfxMessageBox(_T("Open File ERROR"));
				}
				CString sss, aaa;
				sss = "";
				for (int ii = 0; ii < 5; ii++)
				{
					if (sss == "")
					{
						sss = "iDef = 1 农田：";
					}
					else if (sss == "iDef = 1 农田：")
					{
						sss = "iDef = 2 水体：";
					}
					else if (sss == "iDef = 2 水体：")
					{
						sss = "iDef = 3 建筑：";
					}
					else if (sss == "iDef = 3 建筑：")
					{
						sss = "iDef = 4 道路：";
					}
					else if (sss == "iDef = 4 道路：")
					{
						sss = "iDef = 5 其他：";
					}
					int nLengths = sss.GetLength();
					char *cs = new char[nLengths + 1];
					::memset(cs, 0, sizeof(cs));
					::sprintf(cs, "%s", sss);
					file.Write(cs, strlen(cs));
					double ** TempCon = BayesDe.ConMat[ii];
					for (int jj = 0; jj < 3; jj++)
					{
						double * TempTempCon = TempCon[jj];
						for (int kk = 0; kk < 3; kk++)
						{
							aaa.Format("%.2f", TempTempCon[kk]);
							int nLength = aaa.GetLength();
							char *c = new char[nLength + 1];
							::memset(c, 0, sizeof(c));
							::sprintf(c, "%s", aaa);
							file.Write(c, strlen(c));
							for (int oo = 0; oo < 20 - nLength; oo++)
							{
								char *e = new char[2];
								::memset(e, 0, sizeof(e));
								::sprintf(e, "%s", " ");
								file.Write(e, strlen(e));
							}
						}
					}
					char *WriteBuf = "\r\n";
					file.Write(WriteBuf, strlen(WriteBuf));
					file.Write(WriteBuf, strlen(WriteBuf));
				}
			}
		}

		SetWindowText("Bayes分类（计算各个像元分类）");

		BayesDe.MeanValueMat = ExMax.MeanValueMatEM;
		BayesDe.ConMat = ExMax.ConMatEM;

		for (int ii = 0; ii < BayesDe.RasterR.iHeight; ii++)
		{
			BayesDe.Bayes_Decision_Func2(BayesDe.MeanValueMat, BayesDe.ConMat_1, BayesDe.MatResult, ii, BayesDe.XMat, BayesDe.XMatT);
			double pro = int(100.0 / (double)BayesDe.RasterR.iHeight * (double)(ii + 1));//计算进度条进度
			pProg->SetPos(pro);//设置新的进度条进度
		}

		pProg->SetPos(0);//设置新的进度条进度
	}
	else if (ModelFlag == 3)
	{
		BayesDe.Malloc_XMat();
		BayesDe.Malloc_XMatT();

		BayesDe.Malloc_ConMat_1(5, 3);

		BayesDe.Get_Reverse_Matrix_of_Covariance_Matrix(BayesDe.ConMat, BayesDe.ConMat_1);
		BayesDe.Get_Determinant_Matrix_of_Covariance_Matrix(BayesDe.ConMat, BayesDe.MatResult);

		if (EMflag == 1)
		{
			CString sEMTimes;
			CString sEMChangeRate;
			int iEMTimes;
			int iEMChangeRate;

			GetDlgItemTextA(IDC_EDIT_EMTIMES, sEMTimes);
			GetDlgItemTextA(IDC_EDIT_EMCHANGERATE, sEMChangeRate);
			iEMTimes = _ttoi(sEMTimes);
			iEMChangeRate = _ttoi(sEMChangeRate);

			{
				CString FilePathOut = sTifPathOut + "\\MeanValue1.txt";

				CFile file;
				file.Open(FilePathOut, CFile::modeWrite | CFile::modeCreate); //如果不存在就创建

				if (file == NULL)
				{
					AfxMessageBox(_T("Open File ERROR"));
				}
				CString sss, aaa;
				sss = "";
				for (int ii = 0; ii < 5; ii++)
				{
					if (sss == "")
					{
						sss = "iDef = 1 农田：";
					}
					else if (sss == "iDef = 1 农田：")
					{
						sss = "iDef = 2 水体：";
					}
					else if (sss == "iDef = 2 水体：")
					{
						sss = "iDef = 3 建筑：";
					}
					else if (sss == "iDef = 3 建筑：")
					{
						sss = "iDef = 4 道路：";
					}
					else if (sss == "iDef = 4 道路：")
					{
						sss = "iDef = 5 其他：";
					}
					int nLengths = sss.GetLength();
					char *cs = new char[nLengths + 1];
					::memset(cs, 0, sizeof(cs));
					::sprintf(cs, "%s", sss);
					file.Write(cs, strlen(cs));
					double * TempMeanValue = BayesDe.MeanValueMat[ii];

					for (int jj = 0; jj < 3; jj++)
					{
						aaa.Format("%.2f", TempMeanValue[jj]);
						int nLength = aaa.GetLength();
						char *c = new char[nLength + 1];
						::memset(c, 0, sizeof(c));
						::sprintf(c, "%s", aaa);
						file.Write(c, strlen(c));
						for (int oo = 0; oo < 20 - nLength; oo++)
						{
							char *e = new char[2];
							::memset(e, 0, sizeof(e));
							::sprintf(e, "%s", " ");
							file.Write(e, strlen(e));
						}

					}
					char *WriteBuf = "\r\n";
					file.Write(WriteBuf, strlen(WriteBuf));
					file.Write(WriteBuf, strlen(WriteBuf));

				}
			}
			{
				CString FilePathOut = sTifPathOut + "\\ConMat1.txt";

				CFile file;
				file.Open(FilePathOut, CFile::modeWrite | CFile::modeCreate); //如果不存在就创建

				if (file == NULL)
				{
					AfxMessageBox(_T("Open File ERROR"));
				}
				CString sss, aaa;
				sss = "";
				for (int ii = 0; ii < 5; ii++)
				{
					if (sss == "")
					{
						sss = "iDef = 1 农田：";
					}
					else if (sss == "iDef = 1 农田：")
					{
						sss = "iDef = 2 水体：";
					}
					else if (sss == "iDef = 2 水体：")
					{
						sss = "iDef = 3 建筑：";
					}
					else if (sss == "iDef = 3 建筑：")
					{
						sss = "iDef = 4 道路：";
					}
					else if (sss == "iDef = 4 道路：")
					{
						sss = "iDef = 5 其他：";
					}
					int nLengths = sss.GetLength();
					char *cs = new char[nLengths + 1];
					::memset(cs, 0, sizeof(cs));
					::sprintf(cs, "%s", sss);
					file.Write(cs, strlen(cs));
					double ** TempCon = BayesDe.ConMat[ii];
					for (int jj = 0; jj < 3; jj++)
					{
						double * TempTempCon = TempCon[jj];
						for (int kk = 0; kk < 3; kk++)
						{
							aaa.Format("%.2f", TempTempCon[kk]);
							int nLength = aaa.GetLength();
							char *c = new char[nLength + 1];
							::memset(c, 0, sizeof(c));
							::sprintf(c, "%s", aaa);
							file.Write(c, strlen(c));
							for (int oo = 0; oo < 20 - nLength; oo++)
							{
								char *e = new char[2];
								::memset(e, 0, sizeof(e));
								::sprintf(e, "%s", " ");
								file.Write(e, strlen(e));
							}
						}
					}
					char *WriteBuf = "\r\n";
					file.Write(WriteBuf, strlen(WriteBuf));
					file.Write(WriteBuf, strlen(WriteBuf));
				}
			}

			SetWindowText("Bayes分类（高斯混合模型修正）");

			for (int Times = 0; Times < iEMTimes; Times++)
			{
				ExMax.Malloc_pLData(sTifPathIn);
				ExMax.Malloc_MeanValueMat(5, 3);
				ExMax.Malloc_ConMat(5, 3);

				ExMax.Initialize();

				double EndFlag = 0;
				for (int ii = 0; ii < BayesDe.RasterR.iHeight; ii++)
				{
					ExMax.ExpectationMaximization(BayesDe.MeanValueMat, BayesDe.ConMat_1, BayesDe.MatResult, BayesDe.XMat, BayesDe.XMatT, BayesDe.Rate, BayesDe.pCData, BayesDe.pPData, ii);
					double pro = int(100.0 / (double)BayesDe.RasterR.iHeight * (double)(ii + 1));//计算进度条进度
					pProg->SetPos(pro);//设置新的进度条进度
				}

				ExMax.Result();

				BayesDe.MeanValueMat = ExMax.MeanValueMatEM;
				BayesDe.ConMat = ExMax.ConMatEM;

				pProg->SetPos(0);//设置新的进度条进度

				for (int ii = 0; ii < 5; ii++)
				{

					EndFlag += fabs((BayesDe.Rate[ii] - ExMax.RateEM[ii]) / BayesDe.Rate[ii] * 100);

					BayesDe.Rate[ii] = ExMax.RateEM[ii];
				}

				//ExMax.Release();

				if (EndFlag <= 5 * iEMChangeRate && EndFlag >= -5 * iEMChangeRate)
				{
					break;
				}
			}
			
			BayesDe.Get_Reverse_Matrix_of_Covariance_Matrix(BayesDe.ConMat, BayesDe.ConMat_1);
			BayesDe.Get_Determinant_Matrix_of_Covariance_Matrix(BayesDe.ConMat, BayesDe.MatResult);

			{
				CString FilePathOut = sTifPathOut + "\\MeanValue2.txt";

				CFile file;
				file.Open(FilePathOut, CFile::modeWrite | CFile::modeCreate); //如果不存在就创建

				if (file == NULL)
				{
					AfxMessageBox(_T("Open File ERROR"));
				}
				CString sss, aaa;
				sss = "";
				for (int ii = 0; ii < 5; ii++)
				{
					if (sss == "")
					{
						sss = "iDef = 1 农田：";
					}
					else if (sss == "iDef = 1 农田：")
					{
						sss = "iDef = 2 水体：";
					}
					else if (sss == "iDef = 2 水体：")
					{
						sss = "iDef = 3 建筑：";
					}
					else if (sss == "iDef = 3 建筑：")
					{
						sss = "iDef = 4 道路：";
					}
					else if (sss == "iDef = 4 道路：")
					{
						sss = "iDef = 5 其他：";
					}
					int nLengths = sss.GetLength();
					char *cs = new char[nLengths + 1];
					::memset(cs, 0, sizeof(cs));
					::sprintf(cs, "%s", sss);
					file.Write(cs, strlen(cs));
					double * TempMeanValue = BayesDe.MeanValueMat[ii];

					for (int jj = 0; jj < 3; jj++)
					{
						aaa.Format("%.2f", TempMeanValue[jj]);
						int nLength = aaa.GetLength();
						char *c = new char[nLength + 1];
						::memset(c, 0, sizeof(c));
						::sprintf(c, "%s", aaa);
						file.Write(c, strlen(c));
						for (int oo = 0; oo < 20 - nLength; oo++)
						{
							char *e = new char[2];
							::memset(e, 0, sizeof(e));
							::sprintf(e, "%s", " ");
							file.Write(e, strlen(e));
						}

					}
					char *WriteBuf = "\r\n";
					file.Write(WriteBuf, strlen(WriteBuf));
					file.Write(WriteBuf, strlen(WriteBuf));

				}
			}
			{
				CString FilePathOut = sTifPathOut + "\\ConMat2.txt";

				CFile file;
				file.Open(FilePathOut, CFile::modeWrite | CFile::modeCreate); //如果不存在就创建

				if (file == NULL)
				{
					AfxMessageBox(_T("Open File ERROR"));
				}
				CString sss, aaa;
				sss = "";
				for (int ii = 0; ii < 5; ii++)
				{
					if (sss == "")
					{
						sss = "iDef = 1 农田：";
					}
					else if (sss == "iDef = 1 农田：")
					{
						sss = "iDef = 2 水体：";
					}
					else if (sss == "iDef = 2 水体：")
					{
						sss = "iDef = 3 建筑：";
					}
					else if (sss == "iDef = 3 建筑：")
					{
						sss = "iDef = 4 道路：";
					}
					else if (sss == "iDef = 4 道路：")
					{
						sss = "iDef = 5 其他：";
					}
					int nLengths = sss.GetLength();
					char *cs = new char[nLengths + 1];
					::memset(cs, 0, sizeof(cs));
					::sprintf(cs, "%s", sss);
					file.Write(cs, strlen(cs));
					double ** TempCon = BayesDe.ConMat[ii];
					for (int jj = 0; jj < 3; jj++)
					{
						double * TempTempCon = TempCon[jj];
						for (int kk = 0; kk < 3; kk++)
						{
							aaa.Format("%.2f", TempTempCon[kk]);
							int nLength = aaa.GetLength();
							char *c = new char[nLength + 1];
							::memset(c, 0, sizeof(c));
							::sprintf(c, "%s", aaa);
							file.Write(c, strlen(c));
							for (int oo = 0; oo < 20 - nLength; oo++)
							{
								char *e = new char[2];
								::memset(e, 0, sizeof(e));
								::sprintf(e, "%s", " ");
								file.Write(e, strlen(e));
							}
						}
					}
					char *WriteBuf = "\r\n";
					file.Write(WriteBuf, strlen(WriteBuf));
					file.Write(WriteBuf, strlen(WriteBuf));
				}
			}
		}
		
		SetWindowText("Bayes分类（计算各个像元分类）");

		for (int ii = 0; ii < BayesDe.RasterR.iHeight; ii++)
		{
			BayesDe.Bayes_Decision_Func3(BayesDe.MeanValueMat, BayesDe.ConMat_1, BayesDe.MatResult, ii, BayesDe.XMat, BayesDe.XMatT);
			double pro = int(100.0 / (double)BayesDe.RasterR.iHeight * (double)(ii + 1));//计算进度条进度
			pProg->SetPos(pro);//设置新的进度条进度
		}

		pProg->SetPos(0);//设置新的进度条进度
	}

	return 0;
}

BOOL CBayesDlg::Write_Out_Result()
{
	SetWindowText("Bayes分类（导出混淆矩阵）");

	BayesDe.WriteConFuMat(sTifPathOut);
	BayesDe.Raster_Write_Func(sTifPathOut);

	BayesDe.Free_PData_Func(Scan.RasterR.iHeight, Scan.RasterR.iWidth);

	return 0;
}

void CBayesDlg::OnBnClickedRadio1()
{
	ModelFlag = 1;
}

void CBayesDlg::OnBnClickedRadio2()
{
	ModelFlag = 2;
}

void CBayesDlg::OnBnClickedRadio3()
{
	ModelFlag = 3;
}

void CBayesDlg::OnBnClickedCheckEm()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_EM);
	int state = pBtn->GetCheck();
	//上面的也可以绑定一个变量做处理
	if (state == 1) // 选中
	{
		mEMtimes.EnableWindow(1);//设置修改训练集的按钮可用
		mEMchangerate.EnableWindow(1);//设置修改训练集的按钮可用
		EMflag = 1;
	}
	else // 取消选中
	{
		mEMtimes.EnableWindow(0);//设置修改训练集的按钮可用
		mEMchangerate.EnableWindow(0);//设置修改训练集的按钮可用
		EMflag = 0;
	}
}


void CBayesDlg::OnBnClickedCheckFcm()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_FCM);
	int state = pBtn->GetCheck();
	//上面的也可以绑定一个变量做处理
	if (state == 1) // 选中
	{
		mFCMtimes.EnableWindow(1);//设置修改训练集的按钮可用
		mFCMchangerate.EnableWindow(1);//设置修改训练集的按钮可用

		mEMbutton.EnableWindow(0);
		mEMtimes.EnableWindow(0);//设置修改训练集的按钮可用
		mEMchangerate.EnableWindow(0);//设置修改训练集的按钮可用

		mLoss11.EnableWindow(0);
		mLoss12.EnableWindow(0);
		mLoss13.EnableWindow(0);
		mLoss14.EnableWindow(0);
		mLoss15.EnableWindow(0);

		mLoss21.EnableWindow(0);
		mLoss22.EnableWindow(0);
		mLoss23.EnableWindow(0);
		mLoss24.EnableWindow(0);
		mLoss25.EnableWindow(0);

		mLoss31.EnableWindow(0);
		mLoss32.EnableWindow(0);
		mLoss33.EnableWindow(0);
		mLoss34.EnableWindow(0);
		mLoss35.EnableWindow(0);

		mLoss41.EnableWindow(0);
		mLoss42.EnableWindow(0);
		mLoss43.EnableWindow(0);
		mLoss44.EnableWindow(0);
		mLoss45.EnableWindow(0);

		mLoss51.EnableWindow(0);
		mLoss52.EnableWindow(0);
		mLoss53.EnableWindow(0);
		mLoss54.EnableWindow(0);
		mLoss55.EnableWindow(0);

		mtimes.EnableWindow(0);
		mchangerate.EnableWindow(0);

		m_Radio.EnableWindow(0);
		mModel1.EnableWindow(0);
		mModel2.EnableWindow(0);

		FCMflag = 1;
	}
	else // 取消选中
	{
		mFCMtimes.EnableWindow(0);//设置修改训练集的按钮可用
		mFCMchangerate.EnableWindow(0);//设置修改训练集的按钮可用

		mLoss11.EnableWindow(1);
		mLoss12.EnableWindow(1);
		mLoss13.EnableWindow(1);
		mLoss14.EnableWindow(1);
		mLoss15.EnableWindow(1);

		mLoss21.EnableWindow(1);
		mLoss22.EnableWindow(1);
		mLoss23.EnableWindow(1);
		mLoss24.EnableWindow(1);
		mLoss25.EnableWindow(1);

		mLoss31.EnableWindow(1);
		mLoss32.EnableWindow(1);
		mLoss33.EnableWindow(1);
		mLoss34.EnableWindow(1);
		mLoss35.EnableWindow(1);

		mLoss41.EnableWindow(1);
		mLoss42.EnableWindow(1);
		mLoss43.EnableWindow(1);
		mLoss44.EnableWindow(1);
		mLoss45.EnableWindow(1);

		mLoss51.EnableWindow(1);
		mLoss52.EnableWindow(1);
		mLoss53.EnableWindow(1);
		mLoss54.EnableWindow(1);
		mLoss55.EnableWindow(1);

		mtimes.EnableWindow(1);
		mchangerate.EnableWindow(1);

		m_Radio.EnableWindow(1);
		mModel1.EnableWindow(1);
		mModel2.EnableWindow(1);

		mEMbutton.EnableWindow(1);

		if (EMflag == 1)
		{
			mEMtimes.EnableWindow(1);//设置修改训练集的按钮可用
			mEMchangerate.EnableWindow(1);//设置修改训练集的按钮可用
		}

		FCMflag = 0;
	}
}
